package cn.supermarket.controller;

import cn.supermarket.pojo.dto.CategoryAddNewDTO;
import cn.supermarket.pojo.dto.CategoryUpdateDTO;
import cn.supermarket.pojo.entity.FrontCategoryEntity;
import cn.supermarket.pojo.vo.CategoryStandardVO;
import cn.supermarket.pojo.vo.CategoryTreeVO;
import cn.supermarket.service.impl.CategoryServiceImpl;
import cn.supermarket.service.iservice.ICategoryService;
import cn.supermarket.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/category")
@Api(tags = "类别管理")
public class CategoryController extends BaseController{


    @Autowired
    private ICategoryService categoryService;

    @PostMapping("/addNew")
    public JsonResult<Void> addNew(CategoryAddNewDTO categoryAddNewDTO) {
        categoryService.addCategory(categoryAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    public JsonResult<Void> deleteById(Long id) {
        categoryService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/update/{id}")
    public JsonResult<Void> updateById(@PathVariable("id") Long id, CategoryUpdateDTO categoryUpdateDTO) {
        categoryService.updateCategoryById(id, categoryUpdateDTO);
        return JsonResult.ok();
    }


    @PostMapping("/enable")
    public JsonResult setCategoryIsEnableById(@PathVariable("id")Long id){
        categoryService.setCategoryIsEnableById(id);
        return JsonResult.ok();
    }

    @PostMapping("/display")
    public JsonResult setCategoryIsDisplayById(@PathVariable("id")Long id){
        categoryService.setCategoryIsDisplayById(id);
        return JsonResult.ok();
    }

    @PostMapping("/cancel")
    public JsonResult setCategoryIsCancelById(@PathVariable("id")Long id){
        categoryService.setCategoryIsCancelById(id);
        return JsonResult.ok();
    }


    @GetMapping("/select/{id}")
    public JsonResult<CategoryStandardVO>  selectById(@PathVariable("id") Long id){
        CategoryStandardVO categoryStandardVO = categoryService.selectById(id);
        return JsonResult.ok(categoryStandardVO);
    }

    @GetMapping("/select/{name}")
    public JsonResult<CategoryStandardVO>  selectByName(@PathVariable("name") String name){
        CategoryStandardVO categoryStandardVO = categoryService.selectByName(name);
        return JsonResult.ok(categoryStandardVO);
    }

    // 获得分类树信息的方法
    @GetMapping("/all")
    public JsonResult<CategoryTreeVO<FrontCategoryEntity>> categoryTree(){
        // 调用返回即可
        CategoryTreeVO<FrontCategoryEntity> treeVO= categoryService.categoryTree();
        return JsonResult.ok(treeVO);
    }
}
